package cn.iocoder.yudao.module.pickbook.service.collection;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.collection.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.collection.CollectionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.collection.CollectionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 收藏 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private CollectionMapper collectionMapper;

    @Override
    public Long createCollection(CollectionSaveReqVO createReqVO) {
        // 插入
        CollectionDO collection = BeanUtils.toBean(createReqVO, CollectionDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        collection.setId(id);

        collectionMapper.insert(collection);

        // 返回
        return collection.getId();
    }

    @Override
    public void updateCollection(CollectionSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectionExists(updateReqVO.getId());
        // 更新
        CollectionDO updateObj = BeanUtils.toBean(updateReqVO, CollectionDO.class);
        collectionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollection(Long id) {
        // 校验存在
        validateCollectionExists(id);
        // 删除
        collectionMapper.deleteById(id);
    }

    @Override
        public void deleteCollectionListByIds(List<Long> ids) {
        // 删除
        collectionMapper.deleteByIds(ids);
        }


    private void validateCollectionExists(Long id) {
        if (collectionMapper.selectById(id) == null) {
            throw exception(COLLECTION_NOT_EXISTS);
        }
    }

    @Override
    public CollectionDO getCollection(Long id) {
        return collectionMapper.selectById(id);
    }

    @Override
    public PageResult<CollectionDO> getCollectionPage(CollectionPageReqVO pageReqVO) {
        return collectionMapper.selectPage(pageReqVO);
    }

}